/* eslint-disable */
import axios from 'axios'
import { ElLoading } from 'element-plus'
import { ILoadingInstance } from 'element-plus/lib/el-loading/src/loading.type'

declare type RequestFunc = (url:string,data?:any,type?:string) => Promise<any>


const getLoading = () => {
  return ElLoading.service({
    background: 'rgba(0, 0, 0, 0.7)',
    text: '加载中',
    lock: true,
    fullscreen:true
  })
}

const quickRequest: RequestFunc = (reqUrl, data, type) => {
  
  if (typeof reqUrl === 'string') {
    //  第二种请求方式
    const loading = getLoading()
    if (type === 'post') {
      return axios.post(reqUrl, data).then(
        res => {
          loading.close()
          return Promise.resolve(res.data.data)
        }
      )
    } else {
      return axios.get(reqUrl).then(
        res => {
          loading.close()
          return Promise.resolve(res.data)
        }
      )
    }
  } else {
    // 第一中请求方式，可详细配置
    const { url, config, method, data } = reqUrl as RequestProp
    let loading: ILoadingInstance
    if (config?.cover) {
      loading = getLoading()
    }
    return axios.request({
      url,
      method,
      data,
      ...config,
    }).then(
      res => {
        loading?.close()
        return Promise.resolve(res.data)
      }
    )
   }
}

export default quickRequest
